package sk.vrto.web.views;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import sk.vrto.domain.Email;
import sk.vrto.web.presenters.EmailPresenter;

public class EmailView extends VerticalLayout {

    transient private final EmailPresenter presenter;
    transient private Email email;

    public EmailView(EmailPresenter presenter, Email email) {
        this.presenter = presenter;
        this.email = email;
        presenter.setView(this);

        setSpacing(true);
        setStyleName("email-widget");
        setWidth("99%");

        addComponent(new Label("<b>From: </b>" + email.getContact().getEmail(),
                Label.CONTENT_XHTML));
        addComponent(new Label("<b>Title: </b>" + email.getTitle(),
                Label.CONTENT_XHTML));
        addComponent(mailContent());

        Component buttonsBar = buttonsBar();
        addComponent(buttonsBar);
        setComponentAlignment(buttonsBar, Alignment.MIDDLE_RIGHT);
    }

    private Component buttonsBar() {
        HorizontalLayout buttons = new HorizontalLayout();
        buttons.setSpacing(true);
        buttons.setStyleName("email-widget-buttons");

        Component deleteButton = deleButton();
        buttons.addComponent(deleteButton);
        buttons.setComponentAlignment(deleteButton, Alignment.MIDDLE_RIGHT);

        Component replyButton = replyButton();
        buttons.addComponent(replyButton);
        buttons.setComponentAlignment(replyButton, Alignment.MIDDLE_RIGHT);

        Component forwardButton = forwardButton();
        buttons.addComponent(forwardButton);
        buttons.setComponentAlignment(forwardButton, Alignment.MIDDLE_RIGHT);

        return buttons;
    }

    private Component forwardButton() {
        Button forward = new Button();
        forward.setIcon(new ThemeResource("../email/icons/forward.png"));
        forward.setDescription("Forward");
        forward.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                presenter.forward(email);
            }
        });
        return forward;
    }

    private Component replyButton() {
        Button reply = new Button();
        reply.setIcon(new ThemeResource("../email/icons/reply.png"));
        reply.setDescription("Reply");
        reply.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                presenter.reply(email);
            }
        });
        return reply;
    }

    private Component deleButton() {
        Button delete = new Button();
        delete.setIcon(new ThemeResource("../email/icons/delete.png"));
        delete.setDescription("Delete");
        delete.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                presenter.deleteMail(email);
            }
        });
        return delete;
    }

    private Component mailContent() {
        Label mailContent = new Label(email.getContent(), Label.CONTENT_XHTML);
        mailContent.setWidth("100%");
        return mailContent;
    }

    public Email getEmail() {
        return email;
    }
}
